extends PanelContainer


@export var taskScene:PackedScene = preload('res://pages/task/task.tscn')
@onready var taskPanelScene = preload("res://components/task_panel.tscn")
@onready var task_container = %task_container
@onready var scroll_style = preload('res://assets/theme/taskList_scroll.tres')
@onready var v_scroll_bar = %scroll_container

# Called when the node enters the scene tree for the first time.
func _ready():
	Signalbus.task_deled.connect(_on_task_deled)
	Signalbus.task_finished.connect(_on_task_finished)
	show_all_tasks()
	if GlobalVariables.settingData.useWideScrollbar:
		v_scroll_bar.get_v_scroll_bar().custom_minimum_size.x = 25
		scroll_style.content_margin_right = 30
	else:
		scroll_style.content_margin_right = 10
		v_scroll_bar.get_v_scroll_bar().custom_minimum_size.x = 5
		
	v_scroll_bar.add_theme_stylebox_override('panel',scroll_style)
	


func show_all_tasks():
	var tasks = GlobalVariables.load_all_tasks()
	if tasks.size()==0:
		%empty_task_holder.show()
	else:
		%empty_task_holder.hide()
	
	tasks.sort_custom(GlobalUtil.sort_task)
	
	for i in range(tasks.size()):
		var task = tasks[i]
		if not task.isNormal:
			continue
		var taskPanel:PanelContainer = taskPanelScene.instantiate()
		taskPanel.task_data = task
		task_container.add_child(taskPanel)

func _on_new_task_btn_pressed():
	# 创建一个新的action
	var actionData = ActionData.new('新建任务',taskScene,1)
	# 调转到新的页面
	Signalbus.new_action_created.emit(actionData)

func _on_task_deled(_taskEn:TaskEntity):
	var tasks = GlobalVariables.load_all_tasks()
	if tasks.size()==0:
		%empty_task_holder.show()

func _on_task_finished(_taskEn:TaskEntity):
	GlobalUtil.remove_node_children(task_container, [%empty_task_holder])
	show_all_tasks()



